import QQMap from '../../utils/qqMap'
import {
  hotTopic
}
from '../../api/ChatTopicTypeController'
import {
  getYearRewardUserTopN
}
from '../../api/ChatTopicRewardController'
const app = getApp()
let playId = null
Page({
  data: {
    scrollTop: 0.1, //控制在当前页面位置
    loadingError: false, //加载失败
    topicList: [], //话题列表
    topicPage: 0, //帖子总页数

    params: {
      pageNum: 1,
      pageSize: 10,
    }, //帖子参数
    containerHeight: 0,
    isReachBottom: false, //开启上拉加载
    triggered: false, //开启下拉刷新
    city: app.userLocation.city || '暂未选择',
    yearReward: [], //年度打赏排行榜
    searchTopicList: [], //搜索框提示话题列表
    playId: undefined,
    _this: undefined,
  },
  //用户下拉动作
  async onScrollRefresh() {
    try {
      this.setData({
        'params.pageNum': 1
      })
      await this.getTopicList(this.data.params)
    } catch (error) {} finally {
      this.setData({
        triggered: false,
      })
    }
  },
  //上拉刷新
  async onReachBottom() {
    if (this.data.topicPage < this.data.params.pageNum) return
    this.setData({
      isReachBottom: true,
    })
    try {
      await this.getTopicList()
    } catch (error) {

    } finally {
      this.setData({
        isReachBottom: false
      })
    }
  },
  //打开输入框提示
  inputFocus() {
    this.setData({
      focus: true
    })
  },
  //关闭输入框提示
  inputBlur() {
    this.setData({
      focus: false
    })
  },
  //返回页面顶部
  toTop() {
    this.setData({
      scrollTop: 0
    })
  },
  //关闭搜索框提示
  show() {
    // wx.createVideoContext(playId, this).pause()
  },
  //获取话题列表
  async getTopicList() {
    try {
      const data = {
        ...this.data.params,
        userId: app.userId || '1'
      }
      const res = await hotTopic(data)
      if (!this.data.isReachBottom) {
        this.setData({
          topicList: res.list,
          searchTopicList: res.list,
          topicPage: res.totalPage
        })
      } else {
        this.setData({
          topicList: [
            ...this.data.topicList, ...res.list
          ],

        })
      }
      if (this.data.topicPage < this.data.params.pageNum) return
      this.setData({
        'params.pageNum': this.data.params.pageNum + 1
      })
    } catch (error) {
      wx.utils.toast('网络不给力呀')
      this.setData({
        loadingError: true
      })
    }
  },
  //获取年度打赏排行榜
  async getYearReward() {
    const res = await getYearRewardUserTopN(wx.getStorageSync('userId') || '1')
  },
  //获取用户位置
  getLocation() {
    wx.chooseLocation({
      success: (res) => {
        QQMap.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: (res) => {
            const data = {
              provinceCode: res.result.ad_info.adcode,
              cityCode: res.result.ad_info.city_code,
              regionCode: '100',
              province: res.result.address_component.province,
              city: res.result.address_component.city
            }
            wx.setStorageSync('userLocation', data)
            app.userLocation = data
            this.setData({
              city: app.userLocation.city.substring(0, app.userLocation.city.length - 1),
              'params.pageNum': 1,

            })
            this.toTop()
            this.getTopicList()
          }
        })
      }
    })
  },
  //控制视频播放
  play(e) {
    const id = e.detail.id
    const _this = e.detail._this
    if (!this.data.playId) {
      this.setData({
        playId: id,
        _this,
      })
    }
    if (this.data.playId && this.data.playId !== id) {
      wx.createVideoContext('video' + this.data.playId, this.data._this).pause()
      this.setData({
        playId: id,
        _this
      })
    }
  },
  //去搜素
  toSearch() {
    wx.navigateTo({
      url: '/pkgPublish/pages/indexSearch/index',
    })
  },
  onLoad() {
    this.getYearReward()
    this.getTopicList()
    if (app.userLocation.city) return
    //获取用户当前位置
    // wx.getLocation({
    //   type: 'gcj02',
    //   success: (data) => {
    //     //腾讯地图逆地址转换
    //     QQMap.reverseGeocoder({
    //       location: {
    //         latitude: data.latitude,
    //         longitude: data.longitude
    //       },
    //       success: (res) => {
    //         const data = {
    //           provinceCode: res.result.ad_info.adcode,
    //           cityCode: res.result.ad_info.city_code,
    //           regionCode: '100',
    //           province: res.result.address_component.province,
    //           city: res.result.address_component.city
    //         }
    //         wx.setStorageSync('userLocation', data)
    //         app.userLocation = data
    //         this.setData({
    //           city: data.city.substring(0, data.city.length - 1)
    //         })
    //       }
    //     })
    //   }
    // })
  },
  onShow() {
    //接收消息角标
    wx.onSocketMessage((res) => {
      this.getTabBar().setData({
        count: app.count + 1
      })
      app.count = app.count + 1
    })
    if (app.userLocation.city) {
      this.setData({
        city: app.userLocation.city.substring(0, app.userLocation.city.length - 1)
      })
    }
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0,
        count: app.count
      })
    }
  },
})